package com.huiqiao.bus.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huiqiao.bus.domain.Customer;
import com.huiqiao.bus.mapper.CustomerMapper;
import com.huiqiao.bus.mapper.RentMapper;
import com.huiqiao.bus.service.ICustomerService;
import com.huiqiao.bus.vo.CustomerVo;
import com.huiqiao.sys.utils.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-15  15:57
 */
@Service
@Transactional //事务控制
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private RentMapper rentMapper;


    @Override
    public DataGridView queryAllCustomer(CustomerVo customerVo) {
        Page<Object> page = PageHelper.startPage(customerVo.getPage(), customerVo.getLimit());

        Weekend<Customer> weekend = Weekend.of(Customer.class);
        WeekendCriteria<Customer, Object> customerObjectWeekendCriteria = weekend.weekendCriteria();
        if(StrUtil.isNotBlank(customerVo.getCustname())){
            customerObjectWeekendCriteria.andLike(Customer::getCustname, "%" + customerVo.getCustname() + "%");
        }
        if (StrUtil.isNotBlank(customerVo.getAddress())){
            customerObjectWeekendCriteria.andLike(Customer::getAddress, "%" + customerVo.getAddress() + "%");
        }
        if(StrUtil.isNotBlank(customerVo.getPhone())){
            customerObjectWeekendCriteria.andLike(Customer::getPhone, "%" + customerVo.getPhone() + "%");
        }
        if (StrUtil.isNotBlank(customerVo.getIdentity())){
            customerObjectWeekendCriteria.andLike(Customer::getIdentity, "%" + customerVo.getIdentity() + "%");
        }
        if(StrUtil.isNotBlank(customerVo.getCareer())){
            customerObjectWeekendCriteria.andLike(Customer::getCareer,"%" + customerVo.getCareer() + "%");
        }
        if(Objects.nonNull(customerVo.getSex())){
            customerObjectWeekendCriteria.andEqualTo(Customer::getSex, customerVo.getSex());
        }

        List<Customer> data = customerMapper.selectByExample(weekend);

//        List<Customer> data = customerMapper.selectAll();

        return new DataGridView(page.getTotal(), data);
    }


    @Override
    public void addCustomer(CustomerVo customerVo) {
        customerMapper.insertSelective(customerVo);
    }

    @Override
    public void deleteCustomer(String identity) {
        customerMapper.deleteByPrimaryKey(identity);
    }

    @Override
    public void updateCustomer(CustomerVo customerVo) {
        customerMapper.updateByPrimaryKeySelective(customerVo);
    }

    @Override
    public void deleteBatchCustomer(String[] ids) {
        Arrays.asList(ids).forEach(id -> customerMapper.deleteByPrimaryKey(id));
    }

    @Override
    public Object checkCustomerExist(String identity) {
        return customerMapper.selectByPrimaryKey(identity);
    }

    @Override
    public Customer queryCustomerByIdentity(String identity) {
        return customerMapper.selectByPrimaryKey(identity);
    }


}
